package com.wangjie.simple;

/**
 * @author jieshao
 * @date 2022/5/27 11:00
 *
 * 35. 搜索插入位置
 */
public class Test03 {
    public static void main(String[] args) {
        int[] nums = {1, 3, 5, 6};
        int res = searchInsert(nums, 1);
        System.out.println(res);
    }

    /**
     * 寻找左侧边界的二分查找
     *
     * @param nums
     * @param target
     * @return
     */
    public static int searchInsert(int[] nums, int target) {
        int left = 0;
        int right = nums.length;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] >= target) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        return left;
    }
}